package yxy.game.pm2.command.conquest;

import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_冲榜;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ConquestMessage;
import yxy.game.pm2.module.conquest.ConquestHandle;

/** 征战 关卡扫荡询问 */
@MessageType(proto = Command.ConquestSweepOnceAsk, classtype = ConquestMessage.ConquestSweepOnceAskRequest.class)
public class CMD_ConquestSweepOnceAsk extends Command<ConquestMessage.ConquestSweepOnceAskRequest> {

	@Override
	public void execute(MessageSource messageSource, ConquestMessage.ConquestSweepOnceAskRequest request) {
		String playerCode = messageSource.getTag();

		ConquestHandle conquestHandle = new ConquestHandle();
		Result result = conquestHandle.sweepOnceAsk(playerCode);
		if (result.equals(Result.ConquestRewardUpper)) {
			ConquestMessage.ConquestSweepOnceAskResponse.Builder response = ConquestMessage.ConquestSweepOnceAskResponse.newBuilder();
			send(messageSource, response, result);
			return;
		}

		ConquestMessage.ConquestSweepOnceResponse.Builder response = ConquestMessage.ConquestSweepOnceResponse.newBuilder();

		try {
			result = new ConquestHandle().sweepOnce(playerCode, response);

			//todo 每次循环的前半小时内不开启榜
			if(result.equals(Result.Success)){
				ActiveModule_冲榜 am = ActiveModule_冲榜.get(playerCode,false);
				DateTime begin = am.getRunend().minusDays(6).withHourOfDay(0).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
				if(!DateTime.now().isBefore(begin)){
					RankRushHandle.addRankRushData(playerCode, 1, RankRushEnum.冲榜_快速作战.getType());
				}
			}

			send(messageSource, response, result);
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, ConquestMessage.ConquestSweepOnceAskResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

	private void send(MessageSource messageSource, ConquestMessage.ConquestSweepOnceResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}